<?php
namespace app\Models;
require_once "NotORM.php";
class AdminModel {
	public function LoginModel($username, $password) {
		$result = Array();
		if ($username && $password) {
			//链接数据库
			$conn = mysqli_connect("localhost", "root", "123456", "rolesystem");
			$sql = "SELECT username FROM `user` WHERE username=? and `password`=?;";
			// 预处理及绑定
			$stmt = $conn -> prepare($sql);
			//绑定参数
			$stmt -> bind_param("ss", $username, $password);
			//执行
			$stmt -> execute();
			//绑定数据库返回结果
			$stmt -> bind_result($ret);
			$row = $stmt -> fetch();
			if ($ret) {
				$result["error"] = 0;
				$result["message"] = "登录成功";
			} else {
				$result["error"] = 1;
				$result["message"] = "请输入正确的用户名和密码";
			}
			//关闭预处理
			$stmt -> close();
		} else {
			$result["error"] = 1;
			$result["message"] = "用户名和密码不能为空";
		}
		//返回信息
		return $result;
	}

	public function updateTokenModel($username, $token, $expiretime) {
		$pdo = new \PDO("mysql:host=localhost;dbname=rolesystem", "root", "123456");
		$pdo -> exec("SET NAME 'utf-8'");
		$db = new \NotORM($pdo);
		$user = Array();
		$user["token"]=$token;
		$user["expiretime"]=$expiretime;
		return $db -> rolesystem_user("username=?", $username) -> update($user);
	}

}
?>